<?php
 /**
 * Pray Registration
 * @author: trungdt
 * @since: Feb 22, 2011
 * @version: 1.0
 * 
 * Change logs
 * Feb 23, 2011 - trungdt - Add field 
 * Feb 26, 2011 - trungdt - Change register type: from hour/min to hour/min
 * mar 9, 2011 - trungdt - change register type, support select pray ring, and pray time
 */
if (!defined("EXPONENT")) exit("");
    $id=-1;
    $hour=0;
    $min=0;
    if (!exponent_users_isLoggedIn())
    {
        die();
    }
    if (isset($_REQUEST['ring']))
    {
        $ring=_ab_safe_html_string($_REQUEST['ring']);
        $fhour=intval($_REQUEST['fh']);
        $fmin=intval($_REQUEST['fm']);
        $thour=intval($_REQUEST['th']);
        $tmin=intval($_REQUEST['tm']);
        
        // get rings id
        $ring=explode("_",$ring);
        // update pray_register_ring
        $db->delete("pray_register_ring","user_id = {$user->id}");
        $ring_sql=array();
        foreach ($ring as $v)
        {
            if ($v !="")
                $ring_sql[]="({$user->id},{$v})";
        }
        $ring_sql=implode(",",$ring_sql);
        $db->sql("INSERT INTO {$db->prefix}pray_register_ring(user_id, ring_id) VALUES {$ring_sql}");
        // end update pray_register_ring
        
        // add pray time registration
        // convert from time and to time to check the registration is exist or not
        $ftime=$fhour*60+$fmin;
        $ttime=$thour*60+$tmin;
        $temp=$db->selectObjectsBySql("
        SELECT * FROM {$db->prefix}pray_register 
        WHERE  ((fhour*60+fmin <= {$ftime} AND {$ftime} <= thour*60+tmin)
        OR (fhour*60+fmin <= {$ftime} AND {$ttime} <= thour*60+tmin)) 
        AND (user_id = {$user->id})
        ");
        
        if (count($temp)>0){
            die();
        }
        else
        {
            $obj=null;
            $obj->fhour=$fhour;
            $obj->fmin=$fmin;
            $obj->thour=$thour;
            $obj->tmin=$tmin;
            $obj->user_id=$user->id;
            $ret_id=$db->insertObject($obj,"pray_register");
            
            // we dont return HTML because, we dont know that user update register ring or not. refresh page is best choice
            echo "OK";
        }
    }
?>
